package com.haier.cloudremote.protocal;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.haier.cloudremote.common.CityHandler;
import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.model.City;

public class TVProtocal {
	
	/**
	 * get city
	 * 
	 * @return city object
	 */
	public static int getCity() {
		City city = null;
		List<City> citys = parserXMl();
		if (null != citys){
			for (City c : citys) {
				city = c;
			}
			return city.getCityId();
		}else{
			return 000000;
			
			// 以下为测试用
//			city = new City();
//			city.setCityId(110000);
//			city.setCityName("北京");
//			return city;

		}	
		
		
	}
	
	/**
	 * get city list
	 * 
	 * @return city list or null
	 */
	private static List<City> parserXMl() {
		List<City> citys = null;
		City city = null;
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try {
			File file = new File("/data/data/com.android.settings/shared_prefs/cityName.xml");
			SLog.d("ssc", "cityName.xml===="+file.toString());
			if(file.exists()){
				XMLReader reader = factory.newSAXParser().getXMLReader();
				citys = new ArrayList<City>();
				reader.setContentHandler(new CityHandler(citys));
				reader.parse(new InputSource(new FileInputStream(file)));
				for (City c : citys) {
					city = c;
				}
				citys.add(city);
				
			}
			
		} catch (Exception e) {
			SLog.e("ssc", "TVProtocal--------76------e===="+e.getMessage());
			return null;
		}
		return citys;
	}

	
	public static void getXmlString(){
		//声明流对象
	    FileInputStream fis = null;                
	    try{
	    	//创建流对象
	    	fis =new FileInputStream("/data/data/com.android.settings/shared_prefs/cityName.xml");
	    	//读取数据，并将读取到的数据存储到数组中
	    	byte[] data = new byte[1024]; //数据存储的数组
	    	int i= 0; //当前下标
	    	//读取流中的第一个字节数据
	    	int n= fis.read();
	    	//依次读取后续的数据
	    	while(n != -1){ //未到达流的末尾
	    		//将有效数据存储到数组中
	    		data[i] = (byte)n;
	    		//下标增加
	    		i++;
	    		//读取下一个字节的数据
	    		n = fis.read();
	    	}
	    	//解析数据
	    	String s = new String(data,0,i);
	    	//输出字符串
	    	SLog.d("ssc", "cityName.xml===="+s);
	    }catch(Exception e){
	    	if(e!=null){
	    		SLog.e("ssc", "TVProtocal--------108------e===="+e.getMessage());
	    	}
	    	
	    }finally{
	    	 try{
	    		 //关闭流，释放资源
	    		 fis.close();
	    	 }catch(Exception e){
	    		 if(e!=null){
	 	    		SLog.e("ssc", "TVProtocal--------117------e===="+e.getMessage());
	 	    	}
	    	 }
	    }
	}

	/**
	 * 获取当前source
	 * 
	 * @return
	 */
	public static int queryCurInputSrc(Context context) {

		int value = 0;

		Cursor cursor = context.getContentResolver().query(
				Uri.parse("content://mstar.tv.usersetting/systemsetting"),
				null,

				null, null, null);

		if (cursor!=null && cursor.moveToFirst())

		{

			value = cursor.getInt(cursor.getColumnIndex("enInputSourceType"));
			cursor.close();
		}

		

		return value;

	}

}
